/*package com.mstar.qs.dao;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.morningstar.qs.basic.db.bean.SessionUsageTracking;
//import com.morningstar.qs.basic.db.dao.InstitutionService;
//import com.morningstar.qs.basic.db.dao.UserAuthInfoService;
//import com.morningstar.qs.basic.db.dao.base.DefaultBaseDao;
import com.morningstar.qs.basic.db.dto.InstitutionDto;
import com.morningstar.qs.basic.db.dto.SessionUsageTrackingDto;
import com.morningstar.qs.basic.db.dto.UserAuthInfoDto;
import com.morningstar.qs.basic.db.util.BeanBase;

public class UsageTrackingService {

	private final SessionUsageTrackingDAO dao = new SessionUsageTrackingDAO();
	
	public SessionUsageResult search(SearchSessionUsage ssu, int targetPage, int pageSize){
		return dao.search(ssu, targetPage, pageSize);
	}
	
	
	private class SessionUsageTrackingDAO extends DefaultBaseDao<SessionUsageTracking> {
		private SessionUsageTrackingDAO() {
			super(SessionUsageTracking.class);
		}
	
	
		private SessionUsageResult search(SearchSessionUsage ssu, int targetPage, int pageSize){
			StringBuilder sql = new StringBuilder("from SessionUsageTracking rs where 1=1");
			Map<String,Object> paramMap = new HashMap<String, Object>();
			SessionUsageResult rs = new SessionUsageResult();
			if(ssu.getEmail()!=null && !"".equals(ssu.getEmail().trim())){
				UserAuthInfoDto user = new UserAuthInfoService().findByEmail(ssu.getEmail().trim());
				if(user==null){
					return rs;
				}
				int userId = user.getUserId();
				sql.append(" AND rs.UserId=:userId");
				paramMap.put("userId", userId);
			}else if(ssu.getGroupId()>0){
				sql.append(" AND rs.UserId in (select userId from InternalUserGroupMapping as gm where gm.groupId=:groupId)");
				paramMap.put("groupId", ssu.getGroupId());
			}else if(ssu.getInstCode()!=null && !"".equals(ssu.getInstCode().trim())){
				InstitutionDto inst = new InstitutionService().findInstitutionByInstCode(ssu.getInstCode());
				if(inst==null){
					return rs;
				}
				int	instId= inst.getInstId();
				sql.append(" AND rs.UserId in (select userId from InternalUserGroupMapping as gm where gm.groupId in (select groupId from GroupInfo g where g.instId=:instId))");
				paramMap.put("instId", instId);
			}
			if(ssu.getLocale()!=null && !"".equals(ssu.getLocale().trim())){
				sql.append(" AND rs.UserLocale =:locale");
				paramMap.put("locale", ssu.getLocale());
			}
			if(ssu.getBrowser()!=null && !"".equals(ssu.getBrowser().trim())){
				sql.append(" AND rs.UserAgent like '%"+ssu.getBrowser()+"%'");
			}
			if(ssu.getStartTime()!=null && !"".equals(ssu.getStartTime())){
				sql.append(" AND rs.LoginTimestamp >'"+ssu.getStartTime()+"'");
			}
			if(ssu.getEndTime()!=null && !"".equals(ssu.getEndTime())){
				sql.append(" AND rs.LoginTimestamp <'"+ssu.getEndTime()+"'");
			}
			sql.append(" ORDER by rs.LoginTimestamp DESC");
			try {
				Query query = this.getSession().createQuery(sql.toString());
				for(String key : paramMap.keySet()){
					query.setParameter(key, paramMap.get(key));
				}
				query.setFirstResult(pageSize*(targetPage-1));
				query.setMaxResults(pageSize);
				List<SessionUsageTracking> list = query.list();
				if(list!=null && list.size()>0){
					rs.setResultList(BeanBase.convert(list, SessionUsageTrackingDto.class));
				}
				String countSql = "select count(*)" + sql.substring(0, sql.indexOf("ORDER"));
				query= this.getSession().createQuery(countSql);
				for(String key : paramMap.keySet()){
					query.setParameter(key, paramMap.get(key));
				}
				long count =(Long) query.uniqueResult();
				rs.setResultCount((int)count);
				return rs;
			}finally{
				this.closeCurrentSession();
			}
		}
	}
	
	
	public static class SessionUsageResult{
		private List<SessionUsageTrackingDto> resultList;
		private int resultCount;
		
		public List<SessionUsageTrackingDto> getResultList() {
			return resultList;
		}
		public void setResultList(List<SessionUsageTrackingDto> resultList) {
			this.resultList = resultList;
		}
		public int getResultCount() {
			return resultCount;
		}
		public void setResultCount(int resultCount) {
			this.resultCount = resultCount;
		}
		
	}
	
	
	public static class SearchSessionUsage{
		
		private String instCode; 
		private String email;
		private short groupId;
		private String browser;
		private String locale;
		private String startTime;
		private String endTime;
		
		public String getInstCode() {
			return instCode;
		}
		public void setInstCode(String instCode) {
			this.instCode = instCode;
		}
		public String getEmail() {
			return email;
		}
		public void setEmail(String email) {
			this.email = email;
		}
		public short getGroupId() {
			return groupId;
		}
		public void setGroupId(short groupId) {
			this.groupId = groupId;
		}
		public String getBrowser() {
			return browser;
		}
		public void setBrowser(String browser) {
			this.browser = browser;
		}
		public String getLocale() {
			return locale;
		}
		public void setLocale(String locale) {
			this.locale = locale;
		}
		public String getStartTime() {
			return startTime;
		}
		public void setStartTime(String startTime) {
			this.startTime = startTime;
		}
		public String getEndTime() {
			return endTime;
		}
		public void setEndTime(String endTime) {
			this.endTime = endTime;
		}
		
	}
	
	
	public static void main(String args[]){
		UsageTrackingService s = new UsageTrackingService();
		SearchSessionUsage ssu = new SearchSessionUsage();
		SessionUsageResult rs = new SessionUsageResult();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		//ssu.setBrowser("MSIE");
		//ssu.setGroupId((short)4);
		ssu.setInstCode("ING");
		//ssu.setEmail("yusen.liu@morningstar.com");
		ssu.setLocale("GB");
		ssu.setStartTime("2012-03-16");
		ssu.setEndTime("2012-05-31");
	
		rs = s.search(ssu,1,40);
		System.out.println(rs.getResultCount());
		for(SessionUsageTrackingDto dto : rs.getResultList()){
			System.out.println(dto.getUserId()+"\t"+dto.getLoginTimestamp());
		}
	}
	
}
*/